package com.iraeta.controller;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.iraeta.common.AuthAccess;
import com.iraeta.common.Result;
import com.iraeta.entity.Car;
import com.iraeta.service.CarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@RestController(value = "CarController")
@RequestMapping("car")
public class CarController extends BaseController{
    @Autowired
    CarService carService;

    @RequestMapping("listallcar")
    @AuthAccess
    public String listAllCar(@RequestBody JSONObject paramJb){
        String carNo = paramJb.getString("carNo");
        String carType = paramJb.getString("carType");
        Date searchDate = paramJb.getDate("searchDate");
        Date endDate = DateUtil.offsetDay(searchDate, 1);
        List<Car> carList = carService.listCarUse(carNo, carType, searchDate, endDate);
        return JSON.toJSONString(Result.success(carList));
    }

    @RequestMapping("listjncar")
    @AuthAccess
    public String listJnCar(@RequestBody JSONObject paramJb){
        String carNo = paramJb.getString("carNo");
        String carType = paramJb.getString("carType");
        Date searchDate = paramJb.getDate("searchDate");
        Date endDate = DateUtil.offsetDay(searchDate, 1);
        List<Car> carList = carService.listCarUseForJn(carNo, carType, searchDate, endDate);
        return JSON.toJSONString(Result.success(carList));
    }
}
